<template> 
  <div>
  <s-table
    :columns="columns"
    :data="getMockRelatedModelData"
    :loading="loading"
    row-key="id"
    class="h100"
    ref="table">
  </s-table>
  </div>
</template>
<script>
import { STable } from '@/components'
import { mapGetters } from 'vuex'
import modelApi from '@/api/modules/system/modelMana'
import { showRelatedModelColumns } from '../config'
export default {
  components: {
    STable
  },
  props: {
    userId: {
      type: String,
      default: ''
    }
  },
  data () {
    return {
      columns: showRelatedModelColumns,
      modelList: [],
      loading: false
    }
  },
  mounted(){},
  computed: {
    ...mapGetters(['drawerOpenTime']),
  },
  watch: {
    drawerOpenTime: {
      handler () {
        this.resetData()
        // this.getRelatedModelData()
      },
      immediate: true
    }
  },
  methods: {
     getMockRelatedModelData (data={pageNo:1,pageSize:20}) {
        return new Promise((resolve, reject) => {
          this.loading = true
          // 获取已关联的车型
          modelApi.getMyAllModel({ selectAll: false, uId: this.userId })
          .then(res => {
            this.modelList = res.data
            const [index, size, total] = [data.pageNo, data.pageSize, this.modelList.length]
            let len = total / size
            // console.log(index, size, total, 'index, size, total')
            const totalPages = len - parseInt(len) > 0 ? parseInt(len) + 1 : len
            // 截取list
            // const newProduceNewsData = tableList.slice(index * size, (index + 1) * size)
            const newProduceNewsData = this.modelList.slice((index - 1) * size, index * size)
            // console.log(newProduceNewsData, 'newProduceNewsData')
            resolve({
              code: '0',
              message: 'success',
              pageNo: index,
              pageSize: size,
              totalCount: total,
              totalPage: totalPages,
              data:[...newProduceNewsData]
              })
          })
          .catch(e=>reject(e))
          .finally(() => {
            this.loading = false
          })
        })
        
    },
    // 获取已关联的车型
    // getRelatedModelData () {
    //   this.loading = true
    //   return modelApi.getMyAllModel({ selectAll: false, uId: this.userId }).then(res => {
    //     this.modelList = res.data
    //   }).finally(() => {
    //     this.loading = false
    //   })
    // },
    resetData () {
      this.modelList = []
    }
  }
}
</script>
